package cn.itcast.haa.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.image.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * 对话控制器
 *
 * @Author mr.wu
 * @Date 2025-11-7 15:12
 */
@CrossOrigin
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private ChatClient chatClient;

    /**
     * 文生文
     * @param prompt
     * @param chatId
     * @return
     */
    @RequestMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String prompt,String chatId) {
        return chatClient.prompt()
                .user(prompt)
                .advisors(a->a.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream()
                .content();
    }

    @Autowired
    private ImageModel imageModel;

    /**
     * 文生图
     * @param prompt
     * @return
     */
    @GetMapping("/img")
    public String text2Img(String prompt){
        ImageOptions imageOptions = ImageOptionsBuilder.builder().model("qwen-image-plus").build();
        ImagePrompt imagePrompt = new ImagePrompt(prompt, imageOptions);
        ImageResponse imageResponse = imageModel.call(imagePrompt);
        String url = imageResponse.getResult().getOutput().getUrl();
        return url;
    }
}
